package com.application;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.application.base.BaseActivity;
import com.application.databinding.ActivityMainFragmentBinding;
import com.application.databinding.BottomNavLayoutBinding;
import com.application.fragment.FirstFragment;
import com.application.fragment.MainFragment;
import com.application.fragment.FindFragment;
import com.application.util.ARouterConstants;
import com.application.viewmodel.MainViewModel;

@Route(path = ARouterConstants.ACTIVITY_URL_MAIN_FRAGMENT)
public class MainFragmentActivity extends BaseActivity<ActivityMainFragmentBinding, MainViewModel> implements View.OnClickListener {

    // ##################################################################
    // 主界面,由三个fragment组成,实现底部导航栏按钮切换
    // ##################################################################
    private BottomNavLayoutBinding bottomNavLayoutBinding;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    private FirstFragment firstFragment;
    private FindFragment findFragment;
    private MainFragment mainFragment;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewObservable();
        getLoginUsername();
        initEvent();
        initFirstFragment();

    }
    @Override
    protected int initLayoutId() {
        return R.layout.activity_main_fragment;
    }

    @Override
    protected int initVariableId() {
        return BR.mainFragmentViewModel;
    }

    @Override
    protected void initViewDataBinding(Bundle savedInstanceState) {
        super.initViewDataBinding(savedInstanceState);
        binding.setMainFragmentActivity(this);
        bottomNavLayoutBinding=binding.bottomNavLayout;
        fragmentManager=getSupportFragmentManager();
    }

    private void getLoginUsername() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String loginUsername = bundle.getString("username");
            viewModel.setLoginUserName(loginUsername);
            Toast.makeText(this, "欢迎 " + loginUsername + " 登录", Toast.LENGTH_LONG).show();
        }
    }

    private void initEvent() {
        bottomNavLayoutBinding.bottomHome.setOnClickListener(this);
        bottomNavLayoutBinding.bottomFind.setOnClickListener(this);
        bottomNavLayoutBinding.bottomMine.setOnClickListener(this);
    }

    private void initFirstFragment() {
        firstFragment = FirstFragment.newInstance();
        currentFragment = firstFragment;
        fragmentManager.beginTransaction()
                .add(R.id.fcv_main, firstFragment)
                .commit();
        setBottomItemSelected(R.id.bottom_home);
    }

    private void resetBottomState() {
        bottomNavLayoutBinding.ivHome.setSelected(false);
        bottomNavLayoutBinding.tvHome.setTextColor(Color.GRAY);
        bottomNavLayoutBinding.ivFind.setSelected(false);
        bottomNavLayoutBinding.tvFind.setTextColor(Color.GRAY);
        bottomNavLayoutBinding.ivMine.setSelected(false);
        bottomNavLayoutBinding.tvMine.setTextColor(Color.GRAY);
    }

    private void setBottomItemSelected(int id) {
        if (id == R.id.bottom_home) {
            bottomNavLayoutBinding.ivHome.setSelected(true);
            bottomNavLayoutBinding.tvHome.setTextColor(Color.BLACK);
        } else if (id == R.id.bottom_find) {
            bottomNavLayoutBinding.ivFind.setSelected(true);
            bottomNavLayoutBinding.tvFind.setTextColor(Color.BLACK);
        } else if (id == R.id.bottom_mine) {
            bottomNavLayoutBinding.ivMine.setSelected(true);
            bottomNavLayoutBinding.tvMine.setTextColor(Color.BLACK);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        resetBottomState();
        setBottomItemSelected(id);
        fragmentTransaction=fragmentManager.beginTransaction();
        if (id == R.id.bottom_home) {
            if (firstFragment==null){
                firstFragment=FirstFragment.newInstance();
                fragmentTransaction.add(R.id.fcv_main,firstFragment);
            }
            fragmentTransaction.hide(currentFragment).show(firstFragment);
            currentFragment=firstFragment;
        } else if (id == R.id.bottom_find) {
            if (findFragment==null){
                findFragment=FindFragment.newInstance();
                fragmentTransaction.add(R.id.fcv_main,findFragment);
            }
            fragmentTransaction.hide(currentFragment).show(findFragment);
            currentFragment=findFragment;
        } else if (id == R.id.bottom_mine) {
            if (mainFragment==null){
                mainFragment=MainFragment.newInstance(viewModel.getLoginUserName().toString());
                fragmentTransaction.add(R.id.fcv_main,mainFragment);
            }
            fragmentTransaction.hide(currentFragment).show(mainFragment);
            currentFragment=mainFragment;
        }
        fragmentTransaction.commit();
    }

    @Override
    protected void initViewObservable() {
        viewModel.getToastText().observe(this,(String toastText)->{
            if (toastText!=null){
                Toast.makeText(this,toastText,Toast.LENGTH_SHORT).show();
            }
        });
    }

    public ActivityMainFragmentBinding getBinding() {
        return binding;
    }
}